import $H from "@/common/request.js"
import $C from "@/config.js"
export default {
	// 获取当前 时间
	getFormatDateTime(date,isNow) {
		if(isNow){
			let targetTime = date.getTime();
			date = new Date(targetTime)
		}
		else{
			let targetTime = date.getTime() + 60 * 60 * 1000;
			date = new Date(targetTime)
		}
		var seperator1 = "-";
		var seperator2 = ":";
		console.log(date);
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		var strMinutes = date.getMinutes();
		var strSeconds = date.getSeconds();
		var strHours = date.getHours();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		if (strHours >= 0 && strHours <= 9) {
			strHours = "0" + strHours;
		}
		if (strMinutes >= 0 && strMinutes <= 9) {
			strMinutes = "0" + strMinutes;
		}
		if (strSeconds >= 0 && strSeconds <= 9) {
			strSeconds = "0" + strSeconds;
		}
		var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
			" " + strHours + seperator2 + strMinutes +
			seperator2 + strSeconds;
		return currentdate;
	},
	
	// 获取预计配送到的时间
	getReachDate(date,isNow) {
		if(isNow){
			let targetTime = date.getTime() + 60 * 60 * 1000;
			date = new Date(targetTime)
		}
		else{
			let targetTime = date.getTime() - 60 * 60 * 1000;
		date = new Date(targetTime)
		}
		
		console.log(date);
		var seperator1 = "-";
		var seperator2 = ":";
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		var strMinutes = date.getMinutes();
		var strHours = date.getHours();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		if (strHours >= 0 && strHours <= 9) {
			strHours = "0" + strHours;
		}
		if (strMinutes >= 0 && strMinutes <= 9) {
			strMinutes = "0" + strMinutes;
		}
		var currentdate = strHours + seperator2 + strMinutes;
		return currentdate;
	},

	// 跳转页面
	toPage(url) {
		const tabBarPages = [ // TabBar页面列表
			'/pages/index/index',
			'/pages/order/order',
			'/pages/my/my',
		];
		if (tabBarPages.includes(url)) {
			uni.switchTab({
				url
			}); // 使用switchTab进行TabBar页面跳转
		} else {
			uni.navigateTo({
				url
			}); // 使用navigateTo进行非TabBar页面跳转
		}
	},

	// 初始化用户登录状态
	initUser() {
		let u = uni.getStorageSync('userInfo')
		let t = uni.getStorageSync('token')
		if (!u) {
			setTimeout(() => {
				uni.showToast({
					title: '请先登录',
					icon: 'none',
					duration: 2000
				});
			}, 3000);
			this.toPage('/pages/login/login')
		} else {
			//判断token是否过期

		}
		
	},
	getUserInfo() {
		let openId = JSON.parse(uni.getStorageSync('userData')).loginId
		console.log(openId);
		// 后端获取用户信息
		return new Promise((resolve, reject) => {
			$H.get('/wxapi/customers/wx', {
				openId: openId
			}, {
					token: true
				}).then((res) => {
				// 请求成功，将用户信息传递给resolve
				resolve(res);
				uni.setStorageSync('userInfo', JSON.stringify(res))
			}).catch((error) => {
				// 请求失败，将错误信息传递给reject
				reject(error);
			});
		});

	},
	logout() {
		uni.removeStorageSync('userInfo')
		uni.removeStorageSync('phoneNumber')
		uni.removeStorageSync('userData')
		uni.removeStorageSync('token')
		uni.showToast({
			title: '退出成功！',
			icon: 'none',
			duration: 2000,
			mask: true
		});
		setTimeout(function() {
			uni.reLaunch({
				url:'/pages/index/index'
			})
		}, 2000);
	},

	upload(file) {
		console.log(file);
		uni.uploadFile({
			url: $C.baseUrl + '/wxapi/common/upload', // 上传接口地址
			filePath: file, // 选择的文件路径
			name: 'file', // 文件对应的 key 名称
			header: {},
			'content-type': 'multipart/form-data',
			success: function(res) {
				// 3. 上传成功后的回调处理
				let image = JSON.parse(res).url
				console.log('上传成功', image);
				return image;
			},
			fail: function(err) {
				// 上传失败的回调处理
				console.log('上传失败', err);
			}
		});
	},
	formatDateTime(dateTime) {
		const date = new Date(dateTime);
		const month = date.getMonth() + 1;
		const day = date.getDate();
		const hours = date.getHours();
		const minutes = date.getMinutes();
		return month + '-' + day + ' ' + hours + ':' + minutes;
	},

	addLeadingZero(value) {
		return value < 10 ? '0' + value : value;
	},


}